package com.webgis.utils;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.springframework.stereotype.Component;

@Component
public class WFSCapabilitiesGenerator {
      public String createWFSCapabilities() {
        Document document = DocumentHelper.createDocument();
        
        // 创建根元素并直接添加命名空间
        Element root = document.addElement("WFS_Capabilities")
               .addNamespace("wfs", "http://www.opengis.net/wfs")
               .addNamespace("ogc", "http://www.opengis.net/ogc")
               .addNamespace("test", "http://localhost:8888/test")
               .addNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
        
        root.addAttribute("version", "1.0.0");
        root.addAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
        root.addAttribute("xsi:schemaLocation", 
                "http://www.opengis.net/wfs http://schemas.opengis.net/wfs/1.0.0/WFS-capabilities.xsd");

         // Service 元数据
        Element service = root.addElement("Service");
        addElementWithText(service, "Name", "WFS");
        addElementWithText(service, "Title", "Custom WFS Service");
        addElementWithText(service, "Abstract", "This is a custom WFS 1.0.0 implementation");
        addElementWithText(service, "Keywords", "WFS,CUSTOM");
        addElementWithText(service, "OnlineResource", "http://localhost:8888/wfs");
        addElementWithText(service, "Fees", "NONE");
        addElementWithText(service, "AccessConstraints", "NONE");

        // 服务能力描述
        Element capability = root.addElement("Capability");
        Element request = capability.addElement("Request");

        // GetCapabilities 操作
        Element getCapabilities = request.addElement("GetCapabilities");
        Element dcpTypeGC = getCapabilities.addElement("DCPType");
        Element httpGC = dcpTypeGC.addElement("HTTP");
        httpGC.addElement("Get").addAttribute("onlineResource", "http://localhost:8888/wfs?request=GetCapabilities");

        // DescribeFeatureType 操作
        Element describeFeatureType = request.addElement("DescribeFeatureType");
        Element schemaDescriptionLanguage = describeFeatureType.addElement("SchemaDescriptionLanguage");
        schemaDescriptionLanguage.addElement("XMLSCHEMA");
        Element dcpTypeDFT = describeFeatureType.addElement("DCPType");
        Element httpDFT = dcpTypeDFT.addElement("HTTP");
        httpDFT.addElement("Get").addAttribute("onlineResource", "http://localhost:8888/wfs?request=DescribeFeatureType");

        // GetFeature 操作
        Element getFeature = request.addElement("GetFeature");
        Element resultFormat = getFeature.addElement("ResultFormat");
        resultFormat.addElement("GML2");
        resultFormat.addElement("GML3");
        Element dcpTypeGF = getFeature.addElement("DCPType");
        Element httpGF = dcpTypeGF.addElement("HTTP");
        httpGF.addElement("Get").addAttribute("onlineResource", "http://localhost:8888/wfs?request=GetFeature");

        // 图层列表
        Element featureTypeList = root.addElement("FeatureTypeList");
        Element operations = featureTypeList.addElement("Operations");
        operations.addElement("Query");
        operations.addElement("Insert");
        operations.addElement("Update");
        operations.addElement("Delete");
        operations.addElement("Lock");

        // 单个图层定义
        Element featureType = featureTypeList.addElement("FeatureType");
        addElementWithText(featureType, "Name", "test:szry");
        addElementWithText(featureType, "Title", "szry");
        addElementWithText(featureType, "Abstract", "这是一个测试图层");
        addElementWithText(featureType, "Keywords", "features,test");
        addElementWithText(featureType, "SRS", "urn:ogc:def:crs:EPSG::4326");

        // 坐标范围
        Element boundingBox = featureType.addElement("LatLongBoundingBox");
        boundingBox.addAttribute("minx", "90.96869659423828");
        boundingBox.addAttribute("miny", "18.21539878845215");
        boundingBox.addAttribute("maxx", "130.4945068359375");
        boundingBox.addAttribute("maxy", "49.241703033447266");

        // 过滤器能力
        Element filterCapabilities = root.addElement("Filter_Capabilities");
        Element spatialCapabilities = filterCapabilities.addElement("Spatial_Capabilities");
        Element spatialOperators = spatialCapabilities.addElement("Spatial_Operators");
        spatialOperators.addElement("BBOX");

        Element scalarCapabilities = filterCapabilities.addElement("Scalar_Capabilities");
        Element comparisonOperators = scalarCapabilities.addElement("Comparison_Operators");
        comparisonOperators.addElement("Simple_Comparisons");
        
        return document.asXML();
    }
     //方法定义
       private void addElementWithText(Element parent, String name, String text) {
        Element element = parent.addElement(name);
        element.setText(text);
    }
}
